from typing import List

def lower_bound(letters: List[str], target: str):
    left,right=0,len(letters)-1
    while left<=right:
        mid=(left+right)//2
        if letters[mid]>target:
            right=mid-1
        else:
            left=mid+1
    return left
class Solution:
    def nextGreatestLetter(self, letters: List[str], target: str) -> str:
        ans = lower_bound(letters,target)
        if ans==len(letters):
            return letters[0]
        return letters[ans]